package com.orange.quickback.system.service.code.generator.impl;

import com.orange.quickback.system.api.common.result.QBootResult;
import com.orange.quickback.system.dao.code.generator.QBootColumnSetRepository;
import com.orange.quickback.system.entities.code.generator.QBootColumnSet;
import com.orange.quickback.system.service.code.generator.QBootCodeGeneratorService;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class QBootCodeGeneratorServiceImpl implements QBootCodeGeneratorService {
    @Resource
    QBootColumnSetRepository qBootColumnSetRepository;
    @Resource
    private TemplateEngine templateEngine;

    @Override
    public QBootResult getAllColumnSet(int page, int size) {
        List<Sort.Order> orders = new ArrayList<>();
        orders.add(new Sort.Order(Sort.Direction.DESC, "id"));
        Sort sort =Sort.by(orders);
        PageRequest pageRequest = PageRequest.of(page,size,sort);
        return QBootResult.build(qBootColumnSetRepository.findAll(pageRequest));
    }
    @Override
    public QBootResult getTableTemplate(QBootColumnSet qBootColumnSet) {
        Context context = new Context();
        context.setVariable("column_set", qBootColumnSet);
        String tableName = qBootColumnSet.getTableName();
        StringBuilder tableSb = new StringBuilder();
        for(int i=0;i<tableName.length();i++){
            if(tableName.charAt(i)>='A'&&tableName.charAt(i)<='Z'){
                tableSb.append((char) (tableName.charAt(i)-'A'+'a'));
            }
            else {
                tableSb.append(tableName.substring(i));
                break;
            }
        }
        context.setVariable("table_lower_case", tableSb.toString());
        qBootColumnSet.getQBootColumns().forEach(item->{
            String ColumnName = item.getColumnName();
            StringBuilder ColumnNameToLow = new StringBuilder();
            for(int i=0;i<ColumnName.length();i++){
                if(ColumnName.charAt(i)>='A'&&ColumnName.charAt(i)<='Z'){
                    ColumnNameToLow.append((char) (ColumnName.charAt(i)-'A'+'a'));
                }
                else {
                    ColumnNameToLow.append(ColumnName.substring(i));
                    break;
                }
            }
            item.setColumnName(ColumnNameToLow.toString());
        });
        String result = templateEngine.process("TableTemplate", context);
        return QBootResult.build(result);
    }
    @Override
    public QBootResult getEntityTemplate(QBootColumnSet qBootColumnSet) {
        Context context = new Context();
        context.setVariable("column_set", qBootColumnSet);
        String result = templateEngine.process("JavaEntityTemplate", context);
        return QBootResult.build(result);
    }

    @Override
    public QBootResult saveTableTemplate(QBootColumnSet qBootColumnSet) {
        return QBootResult.build(qBootColumnSetRepository.save(qBootColumnSet));
    }

    @Override
    public QBootResult getDAOTemplate(QBootColumnSet qBootColumnSet) {
        Context context = new Context();
        context.setVariable("column_set", qBootColumnSet);
        String result = templateEngine.process("JavaRepositoryTemplate", context);
        return QBootResult.build(result);
    }

    @Override
    public QBootResult getTableApiTemplate(QBootColumnSet qBootColumnSet){
        Context context = new Context();
        context.setVariable("column_set", qBootColumnSet);
        String tableName = qBootColumnSet.getTableName();
        StringBuilder tableSb = new StringBuilder();
        for(int i=0;i<tableName.length();i++){
            if(tableName.charAt(i)>='A'&&tableName.charAt(i)<='Z'){
                tableSb.append((char) (tableName.charAt(i)-'A'+'a'));
            }
            else {
                tableSb.append(tableName.substring(i));
                break;
            }
        }
        context.setVariable("table_lower_case", tableSb.toString());
        String result = templateEngine.process("TableApiRequestTemplate", context);
        return QBootResult.build(result);
    }
    @Override
    public QBootResult getApiControllerTemplate(QBootColumnSet qBootColumnSet){
        Context context = new Context();
        context.setVariable("column_set", qBootColumnSet);
        String tableName = qBootColumnSet.getTableName();
        StringBuilder tableSb = new StringBuilder();
        for(int i=0;i<tableName.length();i++){
            if(tableName.charAt(i)>='A'&&tableName.charAt(i)<='Z'){
                tableSb.append((char) (tableName.charAt(i)-'A'+'a'));
            }
            else {
                tableSb.append(tableName.substring(i));
                break;
            }
        }
        context.setVariable("table_lower_case", tableSb.toString());
        String result = templateEngine.process("JavaControllerTemplate", context);
        return QBootResult.build(result);
    }
    public QBootResult getApiServiceTemplate(QBootColumnSet qBootColumnSet){
        Context context = new Context();
        context.setVariable("column_set", qBootColumnSet);
        String tableName = qBootColumnSet.getTableName();
        StringBuilder tableSb = new StringBuilder();
        for(int i=0;i<tableName.length();i++){
            if(tableName.charAt(i)>='A'&&tableName.charAt(i)<='Z'){
                tableSb.append((char) (tableName.charAt(i)-'A'+'a'));
            }
            else {
                tableSb.append(tableName.substring(i));
                break;
            }
        }
        context.setVariable("table_lower_case", tableSb.toString());
        String result = templateEngine.process("JavaServiceTemplate", context);
        return QBootResult.build(result);
    }
}
